package com.educhains.web.ctrl.frontend;


import com.educhains.module.user.service.UserWalletService;
import com.educhains.module.user.vo.User;
import com.educhains.module.user.vo.UserWallet;
import com.educhains.web.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller()
@RequestMapping("/wallet")
public class WalletCtrl {

    @Autowired
    private UserWalletService userWalletService = null;

    @RequestMapping("/index")
    public String wallet(HttpServletRequest request) {

        User user = SessionUtils.getUser(request);
        request.setAttribute("username", user.getUserName());
        request.setAttribute("userId", user.getUserId());

        return "frontend/wallet/wallet";
    }

    @RequestMapping("/send/{address}")
    public String send(@PathVariable("address") String address, HttpServletRequest request) {
        User user = SessionUtils.getUser(request);
        request.setAttribute("username", user.getUserName());
        request.setAttribute("address", address);
        request.setAttribute("userId", user.getUserId());
        return "frontend/wallet/transfer";
    }

    @RequestMapping("/accounts/{address}")
    public String showAddressInfo(@PathVariable("address") String address, HttpServletRequest request) throws Exception {
        User user = SessionUtils.getUser(request);
        request.setAttribute("username", user.getUserName());
        request.setAttribute("address", address);
        request.setAttribute("userId", user.getUserId());
        return "frontend/wallet/accountInfo";
    }

    @RequestMapping("/transactions/{transactionHash}")
    public String showTransactionInfo(@PathVariable("transactionHash") String transactionHash, HttpServletRequest request) {
        request.setAttribute("transactionHash", transactionHash);
        return "frontend/wallet/transactionDetail";
    }

    @RequestMapping("/saveInfo")
    @ResponseBody
    public String saveAddress(@RequestBody String[] array, HttpServletRequest request) {
        User user = SessionUtils.getUser(request);
        String userId = user.getUserId();

        this.userWalletService.deleteUserWalletById(userId);

        for(String address : array){
            UserWallet userWallet = new UserWallet();
            userWallet.setUserId(userId);
            userWallet.setUserWalletAddress(address);
            this.userWalletService.insert(userWallet);
        }

        List<UserWallet> list = this.userWalletService.getWalletByUserId(userId);
        System.out.println(list.size());

        return "success";
    }


    @RequestMapping("/block/{blockId}")
    public String getBlockInfo(@PathVariable String blockId, HttpServletRequest request) {
        request.setAttribute("blockId", blockId);
        return "frontend/wallet/blockDetail";
    }

}
